// pages/vip/vip.js
const app = getApp()
const connector = require('../../utils/connector.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    gif:"true",//加载动画
    school:true,//是否绑定学校false：未绑定；
    schoolIndex:0,
    array:[],
    authorizationGet:"fasle",//个人信息不全提示
    // gif:"true",
  },
  ceshi(e){
    wx.navigateTo({
      url: './qiniu/qiniu',
    })
  },
  // 选择学校
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      schoolIndex: e.detail.value
    })
  },
  //表单提交
  formSubmit: function (e) {
    var that = this
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let list = e.detail.value
    if (that.data.schoolIndex == 0) {
      wx.showToast({
        title: "请选择驾校",
        icon: 'none',
        duration: 1000
      })
      return false
    }
    list.school_id = that.data.array[that.data.schoolIndex].school_id
    school(that,list)
  },
  //立即进入
  btnSubmit(e){

    // //已购买（理赔）
    // wx.navigateTo({
    //   url: '/pages/shopping/subject/subject',
    // })
    //未购买（购买）
    wx.navigateTo({
      url: '/pages/shopping/subject/subject',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    // vipUserGet(that)
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.setData({
      gif:"true",
    })
    let setTime = setInterval(function(){
      console.log(app.globalData.identitys)
      if(app.globalData.identitys){
        let userIfo = wx.getStorageSync("userInfo")
        // console.log(userIfo)
        playIs(that,userIfo)
        clearInterval(setTime)
      }
    },50)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },
})
//获取所有驾校
function getShool(that){
  connector.getFuncSchool(function(res){
    console.log(res)
    if (res.statusCode == 200){
      that.setData({
        array: res.data
      })
    }else{
      wx.showToast({
        title: res.errMsg,
        icon: 'none',
        duration: 1000
      })
    }
  },function(res){
    console.log(res)
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//学校绑定
function school(that,obj){
  that.setData({
    school:true
  })
  wx.redirectTo({
    url: '/pages/shopping/setMeal/setMeal',
  })
}

//判断用户是否绑定本地区
function addressIs(that,userIfo){
  console.log(userIfo)
  let isAddress = userIfo.address_id
  console.log(isAddress)
  isAddress = 0
  // setTimeout(function(){
    if(isAddress != 0 ){
      wx.redirectTo({
        url: '/pages/shopping/setMeal/setMeal',
      })
    }else{
      connector.getFunc(connector.connector().userIfo, "", "get", app.tokens, function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          wx.setStorageSync("userInfo",res.data.user_info)
          // console.log
          if(res.data.user_info.address_id == 0){
            wx.showModal({
              content: '检测到您还没完善个人信息，是否去完善？',
              confirmText: "确认",
              cancelText: "取消",
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/oneself/profile/redact/redact',
                  })
                } else {
                  wx.switchTab({
                    url: '/pages/shopping/shopping',
                  })
                }
              }
            });
            that.setData({
              gif:"false",
            })
          }else{
            wx.redirectTo({
              url: '/pages/shopping/setMeal/setMeal',
            })
          }
        }
      }, function (res) {
        wx.showToast({
          title: "内部错误",
          icon: 'none',
          duration: 1000
        })
      })
      
    }
}
//判断用户是否购买保险
function playIs(that,userIfo){
  console.log(userIfo)
  // let isPlay = userIfo.vip_status
  app.isPlay = userIfo.vip_status
  console.log(app.isPlay)
  // app.isPlay = 1

  // isPlay = 1 
  if(app.isPlay != 0){
    wx.redirectTo({
      url: '/pages/shopping/subject/notice',
    })
  }else{
    vipUserGet(that,userIfo)
    // addressIs(that,userIfo)
  }
  // setTimeout(function(){
    // if(isPlay != 0){
    //   wx.redirectTo({
    //     url: '/pages/shopping/subject/notice',
    //   })
    // }else{
    //   addressIs(that,userIfo)
    // }
  // },1000)
  
}

//获取用户信息
function vipUserGet(that,userIfo){
  console.log(userIfo)
  // console.log(wx.getStorageSync("users"))
  if(wx.getStorageSync("users") && wx.getStorageSync("users").realname && wx.getStorageSync("users").idcard && wx.getStorageSync("users").mail_address){
    addressIs(that,userIfo)
  }else{
    connector.getFunc(connector.connector().vipUserGet, "", "get", app.tokens, function (res) {
      console.log(res.data)
      if (res.data.result.length < 1) {
      }else{
        console.log(res.data)
        if(res.data.result[0].realname && res.data.result[0].mail_address){
          wx.setStorageSync("users",res.data.result[0])
          addressIs(that,userIfo)
        }else{
          wx.showModal({
            content: '检测到您还没完善个人信息，是否去完善？',
            confirmText: "确认",
            cancelText: "取消",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/oneself/profile/redact/redact',
                })
              } else {
                wx.switchTab({
                  url: '/pages/shopping/shopping',
                })
              }
            }
          });
        }
        
      }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
  }
}

